package cn.edu.zut.hkxy.intercept;

import cn.edu.zut.hkxy.pojo.Admin;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginIntercept implements HandlerInterceptor {
    //Controller执行时
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        System.out.println("->>>>>>>>>>>>>>>1.登录验证");
        HttpSession session=request.getSession();
        Admin admin=(Admin)session.getAttribute("admin");
        if(admin==null){
            System.out.println("1.未登录>>>>>>>跳转");
            response.sendRedirect(request.getContextPath()+"/login");
            return false;
        }else{
            System.out.println("1.已登录>>>>>>>执行下一个拦截器");
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
